var camera, scene, renderer, controls;
var auto = false;
//var auto = true;
var room = {
	//线组
	line:{},
	//墙体形状
	wallZ:(function(){
		var pts = [];
		pts.push( new THREE.Vector2 (0,0) );
		pts.push( new THREE.Vector2 (0,-0.1) );
		pts.push( new THREE.Vector2 (-1,-0.1) );
		pts.push( new THREE.Vector2 (-1,0) );
		return new THREE.Shape( pts );
	})(),
	//墙体形状
	wallX:(function(){
		var pts = [];
		pts.push( new THREE.Vector2 (0,0) );
		pts.push( new THREE.Vector2 (0,1) );
		pts.push( new THREE.Vector2 (0.1,1) );
		pts.push( new THREE.Vector2 (0.1,0) );
		return new THREE.Shape( pts );
	})(),
	//按钮
	addTool : function (data){
		var height = window.innerHeight;
		var toolHtml = "";
		var toolHeight = 0;
		for(var i=0;i<data.length;i++){
			toolHtml += '<div id="'+data[i].id+'" style="width: 40px;height: 40px;line-height: 20px;text-align: center;cursor: pointer;margin: 10px;">'+data[i].name+'</div>';
			toolHeight += 50;
		}
		var top = (height-toolHeight)/2;
		var html = "<div id='_tool' style='background-color:#ffeeff;opacity: 0.7;position: absolute;top:"+top+"px;z-index: 6670;width: 60px;'>";
		$("body").append(html+toolHtml+"</div>");
		$("#_tool div").hover(function(){
			$(this).css({"background-color":"#ffeedd"});
		},function(){
			$(this).css({"background-color":"#ffffff"});
		});
	},
	//拆分数组
	splitData:function (source , size){
		var ind =[];
		var data = [];
		for(var i=0;i<source.length;i++){
		  if(source[i][0] != null){
		      ind.push(i);
		      data.push(source[i]);
		  }
		}
		var result = [];
		for(var i=0;i<size*(source.length-1);i++){
		  result[i] = [null,null,null];
		}
		for(var i = 1;i<ind.length;i++){
		  var start = data[i-1];
		  var end = data[i];
		  var num = (ind[i]-ind[i-1])*size;
		  var jg_x = (end[0]-start[0])/num;
		  var jg_y = (end[1]-start[1])/num;
		  var jg_z = (end[2]-start[2])/num;
		  for(var j=0;j<num;j++){
				result[ind[i-1]*size+j] = [start[0] +jg_x*j,start[1] +jg_y*j,start[2] +jg_z*j];
		  }
		}
		//result[ind[i-1]*size] = data[i-1];
		return result;
	},
	/**
	 * 相机
	 * @param dom
	 */
	setCamera:function(dom){
		camera = new THREE.PerspectiveCamera( 40, dom.width()/dom.height(), 1, 2000 );
//		camera.position.set( 0, 400, 40 * 3.5 );
		camera.position.set( 8, 10, 8 );
	},
	/**
	 * 设置场景
	 */
	setScene:function(){
		var urls = ["5.jpg",	"3.jpg",	"6.jpg",	"4.jpg",	"2.jpg","1.jpg"];
		var reflectionCube = new THREE.CubeTextureLoader()
			.setPath( 'img/' )
			.load( urls );
		reflectionCube.format = THREE.RGBFormat;
		scene = new THREE.Scene();
//		scene.background = reflectionCube;
		scene.add( new THREE.AmbientLight( "#555555") );//环境光源
		light = new THREE.PointLight( "#aaaaaa" );//点光源
		light.position.copy( camera.position );
		light.position.setZ(500);
		scene.add( light );
	},
	/**
	 * 添加文字
	 * @param opt{} text,color,position
	 */
	addText:function(opt){
		var loader = new THREE.FontLoader(); 
		 loader.load("fonts/FZLanTingHeiS-UL-GB_Regular.json", function (res) {
			 var font = new THREE.TextBufferGeometry(opt.text, {
				  font: res,
				  size: 0.20,
				  height: 0.03
			  }); 
			  font.computeBoundingBox(); // 运行以后设置font的boundingBox属性对象，如果不运行无法获得。 
			  font.computeVertexNormals(); 
			  var material = new THREE.MeshLambertMaterial({side:THREE.DoubleSide,color: opt.color}); 
			  fontModel = new THREE.Mesh(font,material); 
			  fontModel.position.set.apply(fontModel.position,opt.position);
//			  fontModel.rotation.x = -1.55;
			  fontModel.rotation.x = - Math.PI / 2;
			  scene.add(fontModel); 
		 }); 
	},
	/**
	 * 添加线 obj:color,positions
	 */
	addLine:function(obj){
		var geometry_line = new THREE.Geometry();   //声明一个几何体
        var material_line = new THREE.LineBasicMaterial({ color: obj.color,linewidth: 5, });   //定义材质外观
        for(var i=0;i<obj.positions.length;i++){
        	var p = obj.positions[i];
        	var point = new THREE.Vector3(p[0],p[1],p[2]);
        	geometry_line.vertices.push(point); //vertices属性里可以存放点对象
        }
        var line = new THREE.Line(geometry_line, material_line);    //定义一条线
        if(typeof room.line[obj.type] == "undefined"){
        	room.line[obj.type] = [];
        }
        room.line[obj.type].push(line);
        scene.add(line);   //加入场景里
	},
	/**
	 * 添加立方体 obj:imgs,lwg,position
	 */
	addCube:function(obj){
		var materials = [];
        for (var i = 0; i < obj.imgs.length; ++i) {
            materials.push(new THREE.MeshBasicMaterial({
                map: THREE.ImageUtils.loadTexture(obj.imgs[i],
                    {}, function() {
                    	renderer.render( scene, camera );
                }),
            overdraw: true
            }));
        }
        var cube = new THREE.Mesh(new THREE.CubeGeometry(obj.lwg[0],obj.lwg[1],obj.lwg[2]),new THREE.MeshFaceMaterial(materials));
        cube.aa = {name:"立方体"};
        cube.position.set.apply(cube.position,obj.position);
        scene.add(cube);
	},
	/**
	 * 画墙体obj :color,points,opa
	 */
	addWall:function(obj){
		var pointsa = [new THREE.Vector3(obj.points[0][0],obj.points[0][1],obj.points[0][2]),new THREE.Vector3(obj.points[1][0],obj.points[1][1],obj.points[1][2])];
		var randomSplinea =  new THREE.CatmullRomCurve3( pointsa );
		//两个点x只相等，z轴方向墙体
		var type = obj.points[0][0] == obj.points[1][0]?room.wallZ:room.wallX;
	    var geometrya = new THREE.ExtrudeGeometry( type, {
	        steps	: 2,
	        extrudePath: randomSplinea
    	});
	  //透明,模拟玻璃
		var matStdParams = {
				roughness: 0.044676705160855, // calculated from shininess = 1000
				color:obj.color,
				metalness: 0.0
			};
		var matStdFloor = new THREE.MeshStandardMaterial( matStdParams );
//	    var materialssa = obj.opa?new THREE.MeshBasicMaterial({color:obj.color,transparent:true,blending:THREE.AdditiveBlending}):new THREE.MeshLambertMaterial({color:obj.color,wireframe: false });
	    var materialssa = obj.opa?new THREE.MeshBasicMaterial({color:obj.color,transparent:true,blending:THREE.AdditiveBlending}):matStdFloor;
		var linea = new THREE.Mesh(geometrya,materialssa);
		scene.add(linea);
	},
	/**
	 * 画地板obj : start,end,color
	 */
	addFloor:function(obj){
		var pts = [];
		pts.push( new THREE.Vector2 (obj.start[0],obj.start[1]) );
		pts.push( new THREE.Vector2 (obj.start[0],obj.end[1]) );
		pts.push( new THREE.Vector2 (obj.end[0],obj.end[1]));
		pts.push( new THREE.Vector2 (obj.end[1],obj.start[0]) );
		var shape = new THREE.Shape( pts );
		
		var pointsa = [new THREE.Vector3(0,-1,0),new THREE.Vector3(0,0,0)];
		var randomSplinea =  new THREE.CatmullRomCurve3( pointsa );
	    var geometrya = new THREE.ExtrudeGeometry( shape, {
	        steps	: 2,
	        extrudePath: randomSplinea
    	});
	    var materialssa = new THREE.MeshLambertMaterial({color:obj.color,wireframe: false });
		var linea = new THREE.Mesh(geometrya,materialssa);
		linea.position.y = -5;
		scene.add(linea);
	},
	/**
	 * 电源进线data color
	 */
	addPowerLines:function(data,color,type){
		for(var i=0;i<data.length;i++){
			room.addLine({color:color,type:type,positions:data[i]});
		}

	},
	//轨迹移动
	startMove:function(list){
		room.addPowerLines([list], "#aaff00","m");
		var data = room.splitData(list,100);
		var start=data[0],end;
		for(var i=0;i<data.length;i++){
			(function(index){
				setTimeout(function(){
					elf.position.set.apply(elf.position,data[index]);
					end = data[index];
					if(index>0){
						if(end[0]==start[0]){
							elf.rotation.z = 3;
							if(scene.rotation.y<2){
								scene.rotation.y += 0.1;
							}
						}
						if(end[2]==start[2]){
							elf.rotation.z = 1.5;
							if(scene.rotation.y>0){
								scene.rotation.y -= 0.1;
							}
						}
					}
					start = data[index];
				},index*50);
			})(i);
		}
		
	},
	/**
	 * 初始化
	 */
	init:function(dom){
		var width = dom.width();
		var height = dom.height();
		init(dom);
		animate();
		function init( dom ) {
			room.setCamera(dom);
			room.setScene();
			
			
//===============================文字2====================================
			 room.addText({text:"机柜A1",color:"#000000",position:[-4,2,-3.5]});
			 room.addText({text:"机柜A2",color:"#ffffff",position:[-2.90,2.00,-3.50]});
			 room.addText({text:"机柜A3",color:"#ffffff",position:[-1.80,2.00,-3.50]});
			 room.addText({text:"机柜A4",color:"#ffffff",position:[-0.70,2.00,-3.50]});
			 
			 room.addText({text:"机柜B1",color:"#ffffff",position:[-4.00,2.00,0.50]});
			 room.addText({text:"机柜B2",color:"#ffffff",position:[-2.90,2.00,0.50]});
			 room.addText({text:"机柜B3",color:"#ffffff",position:[-1.80,2.00,0.50]});
			 room.addText({text:"机柜B4",color:"#ffffff",position:[-0.70,2.00,0.50]});
			 room.addText({text:"机柜B5",color:"#000000",position:[0.40,2.00,0.50]});
//===============================电源走线====================================
            var data = [
							[[ -7.50, 3.50, 4.50],[ -5.50, 2.50, 4.50],[ -4.50, 2.50, 4.50],[-4.50, 2.50, -4.50],[0.10, 2.50, -4.50],[0.10, 0.50, -4.50],[0.10, 0.50, -4.00]],
							[[ -7.50, 3.50, 4.50],[ -5.50, 2.50, 4.50],[ -4.50, 2.50, 4.50],[-4.50, 2.50, -4.50],[-1.00, 2.50, -4.50],[-1.00, 0.50, -4.50],[-1.00, 0.50, -4.00]],
							[[ -7.50, 3.50, 4.50],[ -5.50, 2.50, 4.50],[ -4.50, 2.50, 4.50],[-4.50, 2.50, -4.50],[-2.10, 2.50, -4.50],[-2.10, 0.50, -4.50],[-2.10, 0.50, -4.00]],
							[[ -7.50, 3.50, 4.50],[ -5.50, 2.50, 4.50],[ -4.50, 2.50, 4.50],[-4.50, 2.50, -4.50],[-3.15, 2.50, -4.50],[-3.15, 0.50, -4.50],[-3.15, 0.50, -4.00]],
							
							[[ -7.50, 3.50, 4.50],[ -5.50, 2.50, 4.50],[ -4.50, 2.50, 4.50],[-4.50, 2.50, -2.50],[3.50, 2.50, -2.50],[3.50, 0.50, -2.50],[3.50, 0.50, -2.00]],
							[[ -7.50, 3.50, 4.50],[ -5.50, 2.50, 4.50],[ -4.50, 2.50, 4.50],[-4.50, 2.50, -2.50],[2.40, 2.50, -2.50],[2.40, 0.50, -2.50],[2.40, 0.50, -2.00]],
							[[ -7.50, 3.50, 4.50],[ -5.50, 2.50, 4.50],[ -4.50, 2.50, 4.50],[-4.50, 2.50, -2.50],[1.20, 2.50, -2.50],[1.20, 0.50, -2.50],[1.20, 0.50, -2.00]],
							[[ -7.50, 3.50, 4.50],[ -5.50, 2.50, 4.50],[ -4.50, 2.50, 4.50],[-4.50, 2.50, -2.50],[0.10, 2.50, -2.50],[0.10, 0.50, -2.50],[0.10, 0.50, -2.00]],
							
							[[ -7.50, 3.50, 4.50],[ -5.50, 2.50, 4.50],[ -4.50, 2.50, 4.50],[-4.50, 2.50, -0.50],[1.20, 2.50, -0.50],[1.20, 0.50, -0.50],[1.20, 0.50, 0]],
							[[ -7.50, 3.50, 4.50],[ -5.50, 2.50, 4.50],[ -4.50, 2.50, 4.50],[-4.50, 2.50, -0.50],[0.10, 2.50, -0.50],[0.10, 0.50, -0.50],[0.10, 0.50, 0]],
							[[ -7.50, 3.50, 4.50],[ -5.50, 2.50, 4.50],[ -4.50, 2.50, 4.50],[-4.50, 2.50, -0.50],[-1.00, 2.50, -0.50],[-1.00, 0.50, -0.50],[-1.00, 0.50, 0]],
							[[ -7.50, 3.50, 4.50],[ -5.50, 2.50, 4.50],[ -4.50, 2.50, 4.50],[-4.50, 2.50, -0.50],[-2.10, 2.50, -0.50],[-2.10, 0.50, -0.50],[-2.10, 0.50, 0]],
							[[ -7.50, 3.50, 4.50],[ -5.50, 2.50, 4.50],[ -4.50, 2.50, 4.50],[-4.50, 2.50, -0.50],[-3.15, 2.50, -0.50],[-3.15, 0.50, -0.50],[-3.15, 0.50, 0]],
							
                        ];
			 
            room.addPowerLines(data, "#ff0000","p");
            var data2 = [
                        [[ -7.50, 3.40, 4.50],[ -5.50, 2.40, 4.50],[ -4.50, 2.40, 4.50],[-4.50, 2.40, -4.60],[0, 2.40, -4.60],[0, 0.50, -4.60],[0, 0.50, -4.10]],
                        [[ -7.50, 3.40, 4.50],[ -5.50, 2.40, 4.50],[ -4.50, 2.40, 4.50],[-4.50, 2.40, -4.60],[-1.10, 2.40, -4.60],[-1.10, 0.50, -4.60],[-1.10, 0.50, -4.10]],
                        [[ -7.50, 3.40, 4.50],[ -5.50, 2.40, 4.50],[ -4.50, 2.40, 4.50],[-4.50, 2.40, -4.60],[-2.20, 2.40, -4.60],[-2.20, 0.50, -4.60],[-2.20, 0.50, -4.10]],
                        [[ -7.50, 3.40, 4.50],[ -5.50, 2.40, 4.50],[ -4.50, 2.40, 4.50],[-4.50, 2.40, -4.60],[-3.35, 2.40, -4.60],[-3.35, 0.50, -4.60],[-3.35, 0.50, -4.10]],
                        [[ -7.50, 3.40, 4.50],[ -5.50, 2.40, 4.50],[ -4.50, 2.40, 4.50],[-4.50, 2.40, -2.60],[3.40, 2.40, -2.60],[3.40, 0.50, -2.60],[3.40, 0.50, -2.10]],
                        [[ -7.50, 3.40, 4.50],[ -5.50, 2.40, 4.50],[ -4.50, 2.40, 4.50],[-4.50, 2.40, -2.60],[2.30, 2.40, -2.60],[2.30, 0.50, -2.60],[2.30, 0.50, -2.10]],
                        [[ -7.50, 3.40, 4.50],[ -5.50, 2.40, 4.50],[ -4.50, 2.40, 4.50],[-4.50, 2.40, -2.60],[1.10, 2.40, -2.60],[1.10, 0.50, -2.60],[1.10, 0.50, -2.10]],
                        [[ -7.50, 3.40, 4.50],[ -5.50, 2.40, 4.50],[ -4.50, 2.40, 4.50],[-4.50, 2.40, -2.60],[0, 2.40, -2.60],[0, 0.50, -2.60],[0, 0.50, -2.10]],
                        [[ -7.50, 3.40, 4.50],[ -5.50, 2.40, 4.50],[ -4.50, 2.40, 4.50],[-4.50, 2.40, -0.60],[1.10, 2.40, -0.60],[1.10, 0.50, -0.60],[1.10, 0.50, 0]],
                        [[ -7.50, 3.40, 4.50],[ -5.50, 2.40, 4.50],[ -4.50, 2.40, 4.50],[-4.50, 2.40, -0.60],[0, 2.40, -0.60],[0, 0.50, -0.60],[0, 0.50, 0]],
                        [[ -7.50, 3.40, 4.50],[ -5.50, 2.40, 4.50],[ -4.50, 2.40, 4.50],[-4.50, 2.40, -0.60],[-1.10, 2.40, -0.60],[-1.10, 0.50, -0.60],[-1.10, 0.50, 0]],
                        [[ -7.50, 3.40, 4.50],[ -5.50, 2.40, 4.50],[ -4.50, 2.40, 4.50],[-4.50, 2.40, -0.60],[-2.20, 2.40, -0.60],[-2.20, 0.50, -0.60],[-2.20, 0.50, 0]],
                        [[ -7.50, 3.40, 4.50],[ -5.50, 2.40, 4.50],[ -4.50, 2.40, 4.50],[-4.50, 2.40, -0.60],[-3.25, 2.40, -0.60],[-3.25, 0.50, -0.60],[-3.25, 0.50, 0]],
                        
                        ];
            
            room.addPowerLines(data2, "#00ff00","p");
//            room.startMove([[ -10, 0, 6],[ -1.5, 0, 6],[-1.5, 0, 4],[1, 0, 4],[1, 0, 1.5]])
			 
//================================机柜===================================
            var imgs = [];
            imgs.push('img/d.png');
            imgs.push('img/d.png');
            imgs.push('img/a.png');
            imgs.push('img/a.png');
            imgs.push('img/c.jpg');
            imgs.push('img/b.jpg');
            
            room.addCube({imgs:imgs,lwg:[1,2,1],position:[-4.50+1, 1, -4.50+1]});
            room.addCube({imgs:imgs,lwg:[1,2,1],position:[-4.50+2.10, 1.00, -4.50+1.00]});
            room.addCube({imgs:imgs,lwg:[1,2,1],position:[-4.50+3.20, 1.00, -4.50+1.00]});
            room.addCube({imgs:imgs,lwg:[1,2,1],position:[-4.50+4.30, 1.00, -4.50+1.00]});
//            
            room.addCube({imgs:imgs,lwg:[1,2,1],position:[-4.50+4.30, 1.00, -2.50+1.00]});
            room.addCube({imgs:imgs,lwg:[1,2,1],position:[-4.50+5.40, 1.00, -2.50+1.00]});
            room.addCube({imgs:imgs,lwg:[1,2,1],position:[-4.50+6.50, 1.00, -2.50+1.00]});
            room.addCube({imgs:imgs,lwg:[1,2,1],position:[-4.50+7.60, 1.00, -2.50+1.00]});
//            
            room.addCube({imgs:imgs,lwg:[1,2,1],position:[-4.50+1.00, 1.00, -0.50+1.00]});
            room.addCube({imgs:imgs,lwg:[1,2,1],position:[-4.50+2.10, 1.00, -0.50+1.00]});
            room.addCube({imgs:imgs,lwg:[1,2,1],position:[-4.50+3.20, 1.00, -0.50+1.00]});
            room.addCube({imgs:imgs,lwg:[1,2,1],position:[-4.50+4.30, 1.00, -0.50+1.00]});
            room.addCube({imgs:imgs,lwg:[1,2,1],position:[-4.50+5.40, 1.00, -0.50+1.00]});
            
//============================墙体=======================================
            
            
            room.addWall({color: "#AB8BAB",points:[[-5,0,5],[-5,0,-5]]});
            room.addWall({color: "#AB8BAB",points:[[-5,0,-5],[5,0,-5]]});
            room.addWall({color: "#AB8BAB",points:[[5,0,-5],[5,0,5]]});
            room.addWall({color: "#AB8BAB",points:[[-5,0,5],[-2.5,0,5]]});
            room.addWall({color: "#AB8BAB",points:[[-1,0,5],[5,0,5]]});
            
            room.addWall({color: "#345612",opa:true,points:[[-5,1,3],[-5,1,-3]]});
            room.addWall({color: "#AB8BAB",points:[[-5,1,5],[-5,1,3]]});
            room.addWall({color: "#AB8BAB",points:[[-5,1,-3],[-5,1,-5]]});
            room.addWall({color: "#AB8BAB",points:[[-5,1,-5],[5,1,-5]]});
            room.addWall({color: "#111111",opa:true,points:[[5,1,-3],[5,1,3]]});
            room.addWall({color: "#AB8BAB",points:[[5,1,-5],[5,1,-3]]});
            room.addWall({color: "#AB8BAB",points:[[5,1,3],[5,1,5]]});
            room.addWall({color: "#AB8BAB",points:[[-5,1,5],[-2.5,1,5]]});
            room.addWall({color: "#AB8BAB",points:[[-1,1,5],[5,1,5]]});
            
            room.addWall({color: "#AB8BAB",points:[[-5,2,5],[-5,2,-5]]});
            room.addWall({color: "#AB8BAB",points:[[-5,2,-5],[5,2,-5]]});
            room.addWall({color: "#AB8BAB",points:[[5,2,-5],[5,2,5]]});
            room.addWall({color: "#AB8BAB",points:[[-5,2,5],[5,2,5]]});
            
//==========================地板=========================================
            room.addFloor({start:[-7,-7],end:[7,7],color: "#99aabb"});
            
            
//==========================3的模型人物=========================================
            var loadingManager = new THREE.LoadingManager( function() {
//				scene.add( elf );
//				scene.remove( elf );
			} );
			var loader = new THREE.ColladaLoader( loadingManager );
			loader.load( 'elf2/avatar.json', function ( collada ) {
//				loader.load( 'elf/elf.dae', function ( collada ) {
				elf = collada.scene;
				elf.position.z = 7;
				elf.rotation.z=4.5
			} );
				//==========================3的模型人物girl=========================================
			var loadingManager = new THREE.LoadingManager( function() {
//				scene.add( elf );
//				scene.remove( elf );
			} );
			var loader = new THREE.ColladaLoader( loadingManager );
				loader.load( 'elf/elf.dae', function ( collada ) {
//					loader.load( 'dae/file.dae', function ( collada ) {
				elf = collada.scene;
				elf.position.set(-7,-1,0);
				elf.rotation.z=7
			} );
				
				
				//====================喷泉
				
				var loadingManager = new THREE.LoadingManager( function() {
					scene.add( elf );
				} );
				var loader = new THREE.ColladaLoader( loadingManager );
					loader.load( 'testdae/file.dae', function ( collada ) {
					elf = collada.scene;
					elf.position.set(7,-1,0);
					elf.scale.multiplyScalar(0.5);
				} );
//==========================3的模型城市=========================================
			var manager = new THREE.LoadingManager();
			var texture = new THREE.Texture();
			var loader = new THREE.ImageLoader( manager );
//			loader.load( 'elf/g.jpg', function ( image ) {
			loader.load( 'img/a.png', function ( image ) {
				texture.image = image;
				texture.needsUpdate = true;
			} );

			var loader = new THREE.OBJLoader( manager );
			loader.load( 'elf/city.obj', function ( object ) {
				object.traverse( function ( child ) {
					if ( child instanceof THREE.Mesh ) {
						child.material.map = texture;
					}
				} );
				object.position.set(-4,-35,2);
				object.scale.multiplyScalar(5);
				scene.add( object );
			} );
			
//==========================3的模型小麦=========================================
//			var manager = new THREE.LoadingManager();
//			var texture = new THREE.Texture();
//			var loader = new THREE.ImageLoader( manager );
//			loader.load( 'elf1/a.jpg', function ( image ) {
//				texture.image = image;
//				texture.needsUpdate = true;
//			} );
//			loader.load( 'elf1/b.jpg', function ( image ) {
//				texture.image = image;
//				texture.needsUpdate = true;
//			} );
//			
//			var loader = new THREE.OBJLoader( manager);
//			loader.load( 'elf1/a.obj', function ( object ) {
//				object.traverse( function ( child ) {
//					if ( child instanceof THREE.Mesh ) {
////						child.material.map = texture;
//					     var imgs = [];
//				            imgs.push('img/d.png');
//				            imgs.push('img/d.png');
//				            imgs.push('img/a.png');
//				            imgs.push('img/a.png');
//				            imgs.push('img/c.jpg');
//				            imgs.push('img/b.jpg');
//						child.material.map = THREE.ImageUtils.loadTexture(imgs[4],
//			                    {}, function() {
//			                    	renderer.render( scene, camera );
//			                });
//					}
//				} );
//				object.position.set(1,0,6);
//				object.scale.multiplyScalar(0.005);
//				scene.add( object );
//				
//			} );
////============================mtl模型树=======================================
//			
			THREE.Loader.Handlers.add( /\.dds$/i, new THREE.DDSLoader() );
			var onError = function ( xhr ) { }; 
			var mtlLoader = new THREE.MTLLoader();
			mtlLoader.setTexturePath( 'elf3/' );  
			mtlLoader.setPath( 'elf3/' );  
			mtlLoader.load( 'file.mtl', function( materials ) {  
			    console.log(materials);
				materials.preload();  
				var objLoader = new THREE.OBJLoader();
				objLoader.setMaterials( materials );  
				objLoader.setPath( 'elf3/' );  
				objLoader.load( 'file.obj', function ( object ) {  
				    object.position.set(2, 0, 7);
				    mesh = object;
				    mesh.scale.multiplyScalar(0.001);
				    scene.add( mesh );  
			    } );  
			});
			
//============================3d人物=======================================
			var onProgress = function ( xhr ) {
				if ( xhr.lengthComputable ) {
					var percentComplete = xhr.loaded / xhr.total * 100;
					console.log( Math.round(percentComplete, 2) + '% downloaded' );
				}
			};
			var onError = function ( xhr ) { };
			THREE.Loader.Handlers.add( /\.dds$/i, new THREE.DDSLoader() );
			var mtlLoader = new THREE.MTLLoader();
			mtlLoader.setPath( 'male02/' );
			mtlLoader.load( 'male02.mtl', function( materials ) {
				materials.preload();
				var objLoader = new THREE.OBJLoader();
				objLoader.setMaterials( materials );
				objLoader.setPath( 'male02/' );
				objLoader.load( 'male02.obj', function ( object ) {
					object.scale.multiplyScalar(0.01);
					object.position.set(3, 0, 6);
					scene.add( object );
				}, onProgress, onError );
			});
			
			//============================3d亭子=======================================
			THREE.Loader.Handlers.add( /\.dds$/i, new THREE.DDSLoader() );
			var mtlLoader = new THREE.MTLLoader();
			mtlLoader.setPath( 'obj3/' );
			mtlLoader.load( 'file.mtl', function( materials ) {
				materials.preload();
				var objLoader = new THREE.OBJLoader();
				objLoader.setMaterials( materials );
				objLoader.setPath( 'obj3/' );
				objLoader.load( 'file.obj', function ( object ) {
					object.scale.multiplyScalar(0.001);
					object.position.set(5, 0, 6);
					scene.add( object );
				} );
			});
			//============================3d亭子=======================================
			
			var manager = new THREE.LoadingManager();
			var texture = new THREE.Texture();
			var loader = new THREE.ImageLoader( manager );
			loader.load( 'elf/g.jpg', function ( image ) {
				texture.image = image;
				texture.needsUpdate = true;
			} );

			var loader = new THREE.OBJLoader( manager );
			loader.load( 'obj3/file.obj', function ( object ) {
				object.traverse( function ( child ) {
					if ( child instanceof THREE.Mesh ) {
						child.material.map = texture;
					}
				} );
				object.scale.multiplyScalar(0.001);
				object.position.set(-5, 0, 6);
				scene.add( object );
			} );
			
//============================贴图=======================================
//			var maps = new THREE.TextureLoader().load("post.jpg",function(texture){
//				  renderer.render( scene, camera );
//			  }); 
//			
//			var geometry = new THREE.PlaneGeometry(1,1.5);
//	        var material = new THREE.MeshBasicMaterial({color:"#ffffff",map:maps,depthTest: false,transparent:true});
//	        var rect = new THREE.Mesh(geometry,material);
//	        rect.position.set(1,1.5,5);
//	        scene.add(rect);
	        
	        var imgs = [];
	        imgs.push('img/d.png');
            imgs.push('img/d.png');
            imgs.push('img/a.png');
            imgs.push('img/a.png');
            imgs.push('post.jpg');
            imgs.push('img/b.jpg');
            
            room.addCube({imgs:imgs,lwg:[1,1.5,0.1],position:[1, 1.5, 5]});
	        
//==============================对象拾取=====================================
	        raycaster = new THREE.Raycaster();
	        mouse = new THREE.Vector2();
//============================三棱锥=======================================
	        var geometry = new THREE.CylinderGeometry( 0, 0.1, 0.5, 4, 1 );
//			var material = new THREE.MeshPhongMaterial( { color: "#ff0000", flatShading: true } );
			var material = new THREE.MeshBasicMaterial({color: "#111111",transparent:true,blending:THREE.AdditiveBlending})
			
			
			geometry.rotateX( Math.PI / 2 );
//			var material = new THREE.MeshNormalMaterial();
			
			var mesh = new THREE.Mesh( geometry, material );
			mesh.position.set(1,1,7);
			mesh.updateMatrix();
			mesh.matrixAutoUpdate = false;
//			mesh.lookAt( new THREE.Vector3(0,10,10));
//			mesh.lookAt( new THREE.Vector3(1000,100,100));
			scene.add( mesh );
//================================球形===================================
			var sphereGeometry = new THREE.SphereGeometry( 2, 20, 20 );
//			var sphereMaterial = new THREE.MeshPhongMaterial( { color: '#ff00ff', emissive: "#ff0000" } );
			var sphereMaterial = new THREE.MeshBasicMaterial({color: "#111111",transparent:true,blending:THREE.AdditiveBlending})
			sphere = new THREE.Mesh( sphereGeometry, sphereMaterial );
			sphere.position.set( 0, 3, 0 );
			scene.add( sphere );
//=============================镜子======================================
			var groundMirror = new THREE.Mirror( 12, 12, {
				clipBias: 0.003,
				textureWidth: width * window.devicePixelRatio,
				textureHeight: height * window.devicePixelRatio,
				color: "#551A8B",
				recursion: 1
			} );
			groundMirror.rotateX( - Math.PI / 2 );
			groundMirror.position.y = -5+0.001;
			scene.add( groundMirror );
			
//==============================条形光源ok=====================================
			
			var matStdParams = {
					roughness: 0.044676705160855, // calculated from shininess = 1000
					metalness: 0.0
				};
			var matStdFloor = new THREE.MeshStandardMaterial( matStdParams );
			var geoFloor = new THREE.BoxGeometry( 8, 0.5, 8 );
			mshStdFloor = new THREE.Mesh( geoFloor, matStdFloor );
			mshStdFloor.position.y = 0.5;
			matStdFloor.color.set( 0x808080 );
			scene.add( mshStdFloor );
			
			rectLight = new THREE.RectAreaLight( "#00F5FF", 500, 60, 4 );
			rectLight.position.set( 0, 0, -2 );
			scene.add( rectLight );
			
			
//			rectLight.position.z = -4;
//			for(var i=0;i<300;i++){
//				(function(index){
//					setTimeout(function(){
//						var z = rectLight.position.z;
//						rectLight.position.set(0,0,z+0.1);
//					},50*index);
//				})(i);
//			}
			
//===================================================================
//			renderer = new THREE.WebGLRenderer( { antialias: true } );
			renderer = new THREE.WebGLRenderer( { antialias: true,alpha:true} );
			renderer.setPixelRatio( window.devicePixelRatio );
			renderer.setSize( width,height);
			dom[0].appendChild( renderer.domElement );
			renderer.gammaInput = true;
			renderer.gammaOutput = true;
			renderer.setClearColor( "#999999" );
			controls = new THREE.OrbitControls( camera );
			window.addEventListener( 'resize', onWindowResize, false );
		}
		function onWindowResize() {
			camera.aspect = width/height;
			camera.updateProjectionMatrix();
			renderer.setSize( width,height );
		}
		function animate() {
			requestAnimationFrame( animate );
			if(auto){
				scene.rotation.set(0,scene.rotation._y+0.01,0);
			}
			renderer.render( scene, camera );
		}
	},
	pickupObjects:function (dom,e){
	    //将鼠标点击位置的屏幕坐标转成threejs中的标准坐标
	     mouse.x = (e.clientX/dom.width())*2 -1;
	     mouse.y = -(e.clientY/dom.height())*2 + 1;
	     //从相机发射一条射线，经过鼠标点击位置
	     raycaster.setFromCamera(mouse,camera);
	     //计算射线相机到的对象，可能有多个对象，因此返回的是一个数组，按离相机远近排列
	     let intersects = raycaster.intersectObjects(scene.children);
	     console.log("intersects",intersects);
	     for ( var i = 0; i < intersects.length; i++ ) {
	         //intersects[ i ].object.material.color.set( 0x00ff00 );
	     }
	}
};

